Prevents duplicate events from being generated using DJ and Postgres (#1501)

- Now workers can be scaled independently on Heroku to address extra load
Fixes cantino/huginn#1454

Will Read 8 years ago
parent
commit
a5fbb41b00
1 changed files with 10 additions and 4 deletions
  1. 10 4
      config/initializers/delayed_job.rb

+ 10 - 4
config/initializers/delayed_job.rb

@@ -11,11 +11,17 @@ Delayed::Worker.logger = Rails.logger
11 11
 # Delayed::Worker.logger.level = Logger::DEBUG
12 12
 
13 13
 class Delayed::Job
14
-  scope :pending, ->{ where("locked_at IS NULL AND attempts = 0") }
15
-  scope :awaiting_retry, ->{ where("failed_at IS NULL AND attempts > 0 AND locked_at IS NULL") }
14
+  scope :pending, -> { where("locked_at IS NULL AND attempts = 0") }
15
+  scope :awaiting_retry, -> { where("failed_at IS NULL AND attempts > 0 AND locked_at IS NULL") }
16 16
   scope :failed, -> { where("failed_at IS NOT NULL") }
17 17
 end
18 18
 
19
-Delayed::Backend::ActiveRecord.configure do |config|
20
-  config.reserve_sql_strategy = :default_sql
19
+def database_deadlocks_when_using_optimized_strategy?
20
+  ENV["DATABASE_ADAPTER"] == "mysql2"
21
+end
22
+
23
+if database_deadlocks_when_using_optimized_strategy?
24
+  Delayed::Backend::ActiveRecord.configure do |config|
25
+    config.reserve_sql_strategy = :default_sql
26
+  end
21 27
 end